// shopPages/staffxq/staffxq.js
import time_change from "../../utils/util.js";
var app = getApp()
var page = 1;
var selectedNews = false;
var newsList = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memberSelectList: [{
        name: '今日',
        isSelect: true
      },
      {
        name: '昨日',
        isSelect: true
      },
      {
        name: '本周',
        isSelect: true
      },
      {
        name: '筛选',
        isSelect: true
      },
    ],
    staffselect: 0,
    taskBack: false,
    tabarr: ['跟进', '分享', '订单', '聊天'],
    curren: 0,
    staffid: '', //员工id
    start: '',
    end: '',
    staffname: '',
    staffpic: '',
    list: []

  },
  seleStaff: function(e) {
    var that = this
    page = 1
    newsList = []
    selectedNews = false;
    that.setData({
      staffselect: e.currentTarget.id,
    })
    if (e.currentTarget.id == 3) {
      that.isShow()
    }
    that.getStaff()
  },
  getStaff() {
    var that = this
    var timeList = ['TODAY', 'YESTERDAY', 'WEEK', 'SELECT_DAY'];
    var url = ['/api/1.0/getFollowListByLogType', '/api/1.0/getShareListByLogTypeAndStaffId', '/api/1.0/getOrderListByStaffId', '/api/1.0/getChartListByLogTypeAndStaffId', ]
    that.setData({
      list: []
    });
    wx.request({
      url: app.globalData.bossradar + url[that.data.curren],
      data: {
        staffid: that.data.staffid,
        dateType: timeList[that.data.staffselect],
        startDay: that.data.start,
        endDay: that.data.end,
        page: page,
        pageSize:20
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function(res) {
        console.log(res)
        if (that.data.curren == 2) {
          var timelist1 = [];
          for (var i = 0; i < res.data.data.list.length; i++) {
            var time = time_change.imestampToTime(res.data.data.list[i].payTime)
            res.data.data.list[i].time = time;
            newsList.push(res.data.data.list[i]);
          }
          console.log(newsList)
          if (res.data.data.list.length < 20) {
            selectedNews = false;
          } else {
            selectedNews = true;
            page += 1;
          }
          that.setData({
            list: newsList,
          });
          wx.hideLoading();
        } else {
          newsList = []
          var timelist = [];
          for (var i = 0; i < res.data.data.list.length; i++) {
            if (res.data.data.list[i].createTime){
              var time = time_change.imestampToTime(res.data.data.list[i].createTime)
              res.data.data.list[i].time = time;
            }else{
              var time = time_change.imestampToTime(res.data.data.list[i].updateTime)
              res.data.data.list[i].time = time; 
            }
            
            newsList.push(res.data.data.list[i]);
          }
          console.log(newsList)
          if (res.data.data.list.length < 20) {
            selectedNews = false;
          } else {
            selectedNews = true;
            page += 1;
          }
          that.setData({
            list: newsList,
          });
          wx.hideLoading();
        }

        // that.setData({
        //   newlist: res.data.data.count
        // })
      },
      fail: function(res) {
        console.log(">>>>>>>>>>>失败")
      },
      complete: function(res) {},
    })
  },
  isShow: function() {
    this.setData({
      taskBack: true
    });
  },
  bindDateChange: function(e) {
    this.setData({
      start: e.detail.value
    })
  },
  bindDateChange1: function(e) {
    this.setData({
      end: e.detail.value
    })
  },
  // 筛选确认
  getData11: function() {
    var that = this
    var taskBack = that.data.taskBack
    that.setData({
      taskBack: false
    })
    that.getStaff()
  },
  // 关闭筛选
  getData_hiden() {
    this.setData({
      taskBack: false
    })
  },
  bindtab(e) {
    page = 1
    newsList = []
    selectedNews = false;
    var curren = e.currentTarget.id
    this.setData({
      curren: curren
    })
    this.getStaff()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var day3 = new Date();
    day3.setTime(day3.getTime());
    var month = day3.getMonth() + 1;
    if (month.toString().length == 1) {
      month = '0' + month;
    }
    var day = day3.getDate();
    if (day.toString().length == 1) {
      day = '0' + day;
    }
    var start = day3.getFullYear() + "-" + month + "-01";
    var end = day3.getFullYear() + "-" + month + "-" + day;
    var staff_info = JSON.parse(decodeURIComponent(options.staff));
    that.setData({
      staffid: staff_info.staffid,
      staffname: staff_info.staffname,
      staffpic: staff_info.staffpic,
      start: start,
      end: end
    });
    page = 1
    newsList = []
    selectedNews = false;
    that.getStaff()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (selectedNews == true) {
      var that = this;
      // 显示加载图标  
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1
      that.getStaff()
    } else {
      wx.hideLoading();
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // }
})